package com.test.demo.controller;


import com.test.demo.entity.User;
import com.test.demo.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author 涛
 * @since 2023-01-13
 */
@Api(tags="用户模块")
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 400, message = "请求参数没填好"),
            @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对")
    })
    @ApiOperation(value = "查询所有用户", notes="查询所有用户的接口")
//    查询所有
    @GetMapping("/userList")
    public Result<?> list() {
        List<User> list = userService.list();
        return Result.success(list);
    }

//    查询所有（带分页）
    @ApiOperation(value = "查询用户（带分页）", notes="查询用户（带分页）")
    @GetMapping(value="/page")
    @ResponseBody
//    page就是当前第几页，size是每页多少条数据
//    postman传入page和size的值即可
    public Result<?> page(Integer page ,Integer size) {
        int pageNow = page == null ? 1 : page;
        int pageSize = size == null ? 100 :size;
        int startRows = pageSize*(pageNow-1);
        List<User> list = userService.queryPage(startRows,pageSize);
        int total = list.size();
        HashMap<Object, Object> map = new HashMap<>();
        map.put("records",list);
        map.put("total",total);
        return Result.success(map);
    }

    //    通过id删除
    @ApiOperation(value = "删除用户", notes="删除用户的接口")
    @DeleteMapping("/deleteById")
    public Result<?> delete(Integer id) {
        boolean list = userService.removeById(id);
//        判断改list为true还是false
        if (list) {
            return Result.success();
        } else {
            return Result.error("删除失败");
        }

    }
//    批量删除
    @ApiOperation(value = "批量删除", notes="批量删除")
    @RequestMapping(value = "/dels", method = RequestMethod.POST)
    public Result<?> dels(@RequestBody List<Integer> ids){
        //删除
        if (!ids.isEmpty()){
            userService.dels(ids);
            return Result.success();
        }else {
            return Result.error("删除失败");
        }
    }

    //    通过id查询
    @ApiOperation(value = "通过id查询", notes="通过id查询")
    @GetMapping("/findById")
    public Result<?> find(Integer id) {
        return Result.success(userService.getById(id));
    }

    /**
     * ListByName
     * 通过名字查询单个用户
     * @return
     */
    @ApiOperation(value = "通过输入用户名查询", notes="通过输入用户名查询的接口")
    @GetMapping("/ListByName")
    @ResponseBody
    public Result<?> ListUseByName(String name) {
        List<User> list = userService.findUserByName(name);
        return Result.success(list);
    }



    //    通过传入id和对应参数更新数据
    @ApiOperation(value = "通过传入id和对应参数更新数据", notes="通过传入id和对应参数更新数据")
    @PutMapping("/updateById")
    public Result<?> update( User user) {
        boolean list = userService.updateById(user);
        if (list) {
            return Result.success();
        } else {
            return Result.error("修改失败");
        }
    }

    //    添加数据
    @ApiOperation(value = "添加用户", notes="添加用户")
    @PostMapping("/add")
    public Result<?> add( User user){
        boolean list = userService.save(user);
        if (list) {
            return Result.success(true);
        } else {
            return Result.error("添加失败");
        }
    }
}
